<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>mysql.create.helper</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
        integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>

<body>
    <div class="container">

        <div id="app">
            <div>
                <div class="form-group">
                    <select class="form-control" v-model="action">
                        <option value="Select">Select</option>
                        <option value="Update">Update</option>
                        <option value="Delete">Delete</option>
                        <option value="Insert">Insert</option>
                    </select>
                </div>
                <div class="form-group">
                    <input class="form-control" type="text" v-model="table" placeholder="{tableName}">
                </div>
                <div class="form-group">
                    <input class="form-control" v-model="fields" type="text" placeholder="{fields}">
                </div>
                <div>
                    <textarea name="" id="" class="form-control" rows="20">{{ queryString }}</textarea>
                </div>
            </div>
        </div>

    </div>

    <script type="text/javascript">
        var app = new Vue({
            el: '#app',
            data: {
                table: 'sdk',
                fields: '*',
                action: 'Select'
            },
            methods: {
                Select: function () {
                    var fields = this.fields;
                    fields = this.fields.split(',');
                    fields = fields.join(' ');
                    fields = fields.split(' ');
                    fields = fields.join(',');
                    return "SELECT " + fields + " FROM " + this.table + " WHERE 1 = 1 "
                },
                Update: function () {
                    var fields = this.fields;
                    fields = this.fields.split(',');
                    fields = fields.join(' ');
                    fields = fields.split(' ');                    
                    fields = fields.join('=?,');
                    return "UPDATE " + this.table + " SET " + fields + " WHERE 1 = 1 "
                },
                Delete: function () {
                    return "DELETE  FROM " + this.table + " WHERE 1 = 1 "
                },
                Insert: function () {
                    var fields = this.fields;
                    fields = this.fields.split(',');
                    fields = fields.join(' ');
                    fields = fields.split(' ');
                    var values = Array(fields.length).join('?,');
                    fields = fields.join(',')
                    fields = fields.substr(0, fields.length - 1);
                    values = values.substr(0, values.length - 1);
                    return "INSERT INTO " + this.table + " (" + fields + ") VALUES (" + values + ") "
                }
            },
            computed: {
                queryString: function () {
                    return this[this.action]();
                }
            }
        });
    </script>
</body>

</html>